package com.danco.training.zaneuskaya.controller.book;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.danco.training.zaneuskaya.api.BookService;
import com.danco.training.zaneuskaya.controller.GenericController;
import com.danco.training.zaneuskaya.domain.Book;
import com.danco.training.zaneuskaya.service.impl.BookServiceImpl;

@SuppressWarnings("serial")
public class BookInfoController extends GenericController{


	
	private BookService bookService;
	private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	
	
	public BookInfoController() {
		super();
		bookService = BookServiceImpl.getBookStorageSeviceImplInstance();
	}
	
		
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String id = req.getParameter("id");

		if (id != null && !id.isEmpty()) {
			Book book = bookService.getById(Integer.parseInt(id));
			req.setAttribute("book", book);
			req.setAttribute("pub_date",
					formatter.format(book.getPublicationDate()));
		}

		req.getRequestDispatcher("/WEB-INF/views/book/info.jsp").forward(
						req, resp);
		
	}
	

}
